<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="订单编号" prop="orderNum">
          <el-input
            v-model="queryParams.orderNum"
            placeholder="请输入订单编号"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="制单人" prop="orderPerson">
          <el-input
            v-model="queryParams.orderPerson"
            placeholder="请输入制单人"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable style="width: 200px;">
            <el-option
              v-for="dict in order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['medicine:order:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="订单ID" align="center" prop="orderId" />
        <el-table-column label="订单编号" align="center" prop="orderNum">
          <template #default="scope">
                 <router-link :to="'/medicine/detail/orderDetail/'+scope.row.orderId" class="link-type">
                    <span>{{ scope.row.orderNum }}</span>
                 </router-link>
              </template>
        </el-table-column>
        <el-table-column label="制单人" align="center" prop="user.userName" />
        <el-table-column label="供应商" align="center" prop="supplier.supplierName" />
        <el-table-column label="订单状态" align="center" prop="orderStatus">
          <template #default="scope">
            <dict-tag :options="order_status" :value="scope.row.orderStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作"  align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" v-if="scope.row.orderStatus==0"icon="Edit" @click="handleAudit(scope.row)" v-hasPermi="['medicine:order:edit']">点击审核</el-button>
            <el-button link type="primary" v-if="scope.row.orderStatus==3"icon="Edit" @click="handleAuditZf(scope.row)" v-hasPermi="['medicine:order:edit']">点击作废</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
          <!-- 审核对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="orderRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核结果" prop="auditResult">
          <el-select v-model="form.auditResult" placeholder="请选择审核结果">
            <el-option
              v-for="dict in audit_result"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核意见" prop="auditContent">
          <el-input v-model="form.auditContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    </div>
  </template>
  
  <script setup name="Order">
  import { listAuditOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/his/order";
  import { addAudit } from "@/api/his/audit";
  const router = useRouter();
  const { proxy } = getCurrentInstance();
  const { order_status,audit_result} = proxy.useDict('order_status','audit_result');
  
  const orderList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  
  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      orderNum: null,
      orderPerson: null,
      supplierId: null,
      orderStatus: null,
    },
    rules: {
      supplierId: [
        { required: true, message: "供应商ID不能为空", trigger: "change" }
      ],
    }
  });
  
  const { queryParams, form, rules } = toRefs(data);
  
  /** 查询采购管理列表 */
  function getList() {
    loading.value = true;
    listAuditOrder(queryParams.value).then(response => {
      orderList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }
  
  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }
  
  // 表单重置
  function reset() {
    form.value = {
      orderId: null,
      auditResult: null,
      auditContent: null
    };
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  
  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.orderId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }
  
  /** 新增按钮操作 */
  function handleAdd() {
    router.push('/medicine/orderAdd/addOrder')
  }
  
  /** 审核操作 */
  function handleAudit(row) {
    reset();
    form.value.orderId=row.orderId;
    open.value=true;
    title.value="审核采购单"
  }
  
  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["orderRef"].validate(valid => {
      if (valid) {
          addAudit(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
      }
    });
  }
  
  /** 删除按钮操作 */
  function handleDelete(row) {
    const _orderIds = row.orderId || ids.value;
    proxy.$modal.confirm('是否确认删除采购管理编号为"' + _orderIds + '"的数据项？').then(function() {
      return delOrder(_orderIds);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }
  
  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('medicine/order/export', {
      ...queryParams.value
    }, `order_${new Date().getTime()}.xlsx`)
  }
   //查看我的采购
   function handleOrderCart(){
      router.push('/medicine/cart/cartInfo')
    }
    //审核作废
  function  handleAuditZf(row){
    const data={
      orderId: row.orderId,
      auditResult:4
    }
    proxy.$modal.confirm('是否确认作废采购编号为"' + row.orderNum + '"的采购单？').then(function() {
      return addAudit(data);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("作废成功");
    }).catch(() => {});
  }
  getList();
  </script>
  